/* Moral, Mert, H. Ege �zen, and Efe Tokdemir. 
�Bringing the Incumbency Advantage into Question for Proportional Representation� 
Electoral Studies 40: 56-65
Replication File
07.24.2015 */

*** DATA OPERATIONS
cd "/Users/mmoral/Desktop/Replication/" 
** Election Data
import excel "Raw Data.xlsx", sheet("Elections-Stacked") firstrow case(lower) clear
* Party 
encode party, gen(party2)
lab define par1 1 "AKP" 2 "ANAP" 3 "BDP" 4 "Indeps" 5 "CHP" 6 "DEHAP" 7 "DP" 8 "DSP" 9 "DYP" 10 "FP" 11 "GP" 12 "HADEP" 13 "HP" 14 "MCP" 15 "MDP" 16 "MHP" 17 "RP" 18 "SHP" 19 "SP"
lab val party2 par1
* Election year
egen float year = group(term)
* Party ID
gen parid=district*100+party2
saveold "Elections Raw.dta", replace

** Volatility Data
import excel "Raw Data.xlsx", sheet("Elections-Aggregate") firstrow case(lower) clear
recode _all (0/0.01=0) (missing=0)
foreach var of varlist anap-mmp {
qui count if `var'<0.02      
if r(N)==_N $p drop `var'
}
egen others= rowtotal(anap-akp)
replace others=1-others 
foreach var of varlist anap-others {  
gen `var'vb=abs(`var'-`var'[_n-1]) if `var'[_n-1]!=0 & `var'!=0
gen `var'va=`var' if `var'[_n-1]==0 
gen `var'vc=`var'[_n-1] if `var'[_n-1]!=0 & `var'==0
}
egen typea=rowtotal(*va *vc)
egen typeb=rowtotal(*vb)
foreach var of varlist type* {
replace `var'=`var'/2
}
gen vol=typea+typeb
keep term typea typeb vol
saveold "All Elections_Aggregate.dta", replace

** MP Data 
import excel "Raw Data.xlsx", sheet("MP Coding") firstrow case(lower) clear
* Party
encode party1, gen(party)
recode party (6=8) (7=9) (8=10) (9=16) (10=17) (11=18), gen(party2)
lab define par1 1 "AKP" 2 "ANAP" 3 "BDP" 4 "Indeps" 5 "CHP" 6 "DEHAP" 7 "DP" 8 "DSP" 9 "DYP" 10 "FP" 11 "GP" 12 "HADEP" 13 "HP" 14 "MCP" 15 "MDP" 16 "MHP" 17 "RP" 18 "SHP" 19 "SP"
lab val party2 par1
* District
encode district, gen(district1)
recode district1 (1=1) (2=2) (3=3) (4=4) (5=68) (6=5) (7=6) (8=601) (9=602) (10=7) (11=75) (12=8) (13=9) (14=10) (15=74) (16=72) (17=69) (18=11) (19=12) (20=13) (21=14) (22=15) (23=16) (24=17) (25=18) (26=19) (27=20) (28=21) (29=81) (30=22) (31=23) (32=24) (33=25) (34=26) (35=27) (36=28) (37=29) (38=30) (39=31) (40=76) (41=32) (42=34) (43=341) (44=342) (45=343) (46=35) (47=351) (48=352) (49=46) (50=78) (51=70) (52=36) (53=37) (54=38) (55=79) (56=71) (57=39) (58=40) (59=41) (60=42) (61=43) (62=44) (63=45) (64=47) (65=33) (66=48) (67=49) (68=50) (69=51) (70=52) (71=80) (72=53) (73=54) (74=55) (75=63) (76=56) (77=57) (78=73) (79=58) (80=59) (81=60) (82=61) (83=62) (84=64) (85=65) (86=77) (87=66) (88=67)
lab define dis 1 " Adana" 2 " Ad�yaman" 3 " Afyon" 4 " A?r�" 68 " Aksaray" 5 " Amasya" 6 " Ankara" 601 "Ankara-1" 602 "Ankara-2" 7 " Antalya" 75 " Ardahan" 8 " Artvin" 9 " Ayd�n" 10 " Bal�kesir" 74 " Bart�n" 72 " Batman" 69 " Bayburt" 11 " Bilecik" 12 " Bing�l" 13 " Bitlis" 14 " Bolu" 15 " Burdur" 16 " Bursa" 17 " �anakkale" 18 " �ank�r�" 19 " �orum" 20 " Denizli" 21 " Diyarbak�r" 81 " D�zce" 22 " Edirne" 23 " Elaz�?" 24 " Erzincan" 25 " Erzurum" 26 " Eski?ehir" 27 " Gaziantep" 28 " Giresun" 29 " G�m�?hane" 30 " Hakkari" 31 " Hatay" 76 " I?d�r" 32 " Isparta" 34 " ?stanbul" 341 "Istanbul-1" 342 "Istanbul-2" 343 "Istanbul-3" 35 " ?zmir" 351 "Izmir-1" 352 "Izmir-2" 46 " Kahramanmara?" 78 " Karab�k" 70 " Karaman" 36 " Kars" 37 " Kastamonu" 38 " Kayseri" 79 " Kilis" 71 " K�r�kkale" 39 " K�rklareli" 40 " K�r?ehir" 41 " Kocaeli" 42 " Konya" 43 " K�tahya" 44 " Malatya" 45 " Manisa" 47 " Mardin" 33 " Mersin" 48 " Mu?la" 49 " Mu?" 50 " Nev?ehir" 51 " Ni?de" 52 " Ordu" 80 " Osmaniye" 53 " Rize" 54 " Sakarya" 55 " Samsun" 63 " ?anl�urfa" 56 " Siirt" 57 " Sinop" 73 " ?�rnak" 58 " Sivas" 59 " Tekirda?" 60 " Tokat" 61 " Trabzon" 62 " Tunceli" 64 " U?ak" 65 " Van" 77 " Yalova" 66 " Yozgat" 67 " Zonguldak"
lab val district1 dis
drop district
rename district1 district
* Count of MPs for each party list
by party term district, sort : egen float cntmp = count(mpid)
* Count of both measures of incumbency for each party list
rename incumbent inc
replace inc=inc-apas
foreach var of varlist inc apas {
by party term district, sort : egen float cnt`var' = sum(`var')
replace cnt`var' = cnt`var'/cntmp
}

** Replication Data
collapse (mean) cnt*, by(term district party2)
gen parid=district*100+party2
* Merging w/ Election Data
merge m:1 parid term using "Elections Raw.dta"
rename _merge _merge1
* Merging w/ Volatility data
merge m:m term using "All Elections_Aggregate.dta"
* District magnitude
by district term, sort : egen float totmp = total(cntmp)
* Effective number of legislative parties in district (seats)
gen seatsq=(cntmp/totmp)^2
by district term, sort : egen float enlp = total(seatsq)
replace enlp=1/enlp
* Party ID (for robustness checks)
gen parid1=(district*10000)+party2
* Dropping 1983 general elections, extra-parliamentary parties
drop if term==1983 | missing(cntmp)
* Dropping Bayburt (Dis. Magnitude=1 in 2011)
drop if totmp==1
* Dropping Independents with no party affiliation (except BDP in 2007 & 2011, see Footnote#5)
drop if party2==4
* Dropping auxiliary variables
drop party parid seatsq _m*
* Variable labels
lab var cntmp "Total Number of MPs"
lab var cntinc "TRINC/MPs"
lab var cntapas "SPSDINC/MPs"
lab var totmp "District Magnitude"
lab var avvs "Average Vote Share"
lab var enlp "ENLP (Seats)"
lab var parid1 "Party ID"
lab var term "Election Year" 
lab var typea "Type A Volatility"
lab var typeb "Type B Volatility"
lab var vol "Total Volatility"
saveold "Replication.dta", replace

*** ANALYSES
use "Replication.dta", clear
** Figure 1 (p.60)
sort term
twoway (connected typea term, mcolor(black) lpattern(shortdash)) (connected typeb term, mcolor(black)) (connected vol term, mcolor(black) lpattern(line)) , legend(symxsize(6) forcesize cols(3)) ylabel(0(.15).75) xlabel(1987 1991 1995 1999 2002 2007 2011) scale(.85)
graph export Figure1.eps, replace
** Table 1 (p.61)
eststo m1: reg voteshare cntapas totmp avvs enlp, robust cluster(district)
eststo m2: reg voteshare cntapas totmp avvs enlp if term!=2002 & term!=1987, robust cluster(district)
eststo m3: reg voteshare cntapas totmp avvs enlp if term==2002 | term==1987, robust cluster(district)
eststo m4: reg voteshare cntinc totmp avvs enlp, robust cluster(district)
eststo m5: reg voteshare cntinc totmp avvs enlp if term!=2002 & term!=1987, robust cluster(district)
eststo m6: reg voteshare cntinc totmp avvs enlp if term==2002 | term==1987, robust cluster(district)
esttab m1 m2 m3 m4 m5 m6 using "table1.tex", tex replace b(%10.3f) se scalars("r2 \$R^2\$") label mtitles starlevels(* 0.10 ** 0.05  *** 0.01) varlabels(_cons Constant)
** Table 2 (p.61)
* Fake district magnitude 
gen totmp2=_n+1 in 1/49
label variable totmp2 "District Magnitude (Out-of-sample)"
foreach x in apas inc {
eststo `x': reg voteshare totmp cnt`x' c.totmp#c.cnt`x' avvs enlp, robust cluster(district)
mat b=e(b)
mat V=e(V)
sca b2=b[1,2]
sca b3=b[1,3]
sca varb2=V[2,2]
sca varb3=V[3,3]
sca covb2b3=V[2,3]
gen margs`x'=b2+(b3*totmp2)
gen ses`x'=sqrt(varb2+((totmp2^2)*varb3)+(2*totmp2*covb2b3))
gen tes`x'=margs`x'/ses`x'
gen ub`x'=margs`x'+invttail(_N,.05)*ses`x'
gen lb`x'=margs`x'+invttail(_N,.95)*ses`x'
}
esttab apas inc using "table2.tex", tex replace b(%10.3f) se scalars("r2 \$R^2\$") label mtitles starlevels(* 0.10 ** 0.05  *** 0.01) varlabels(_cons Constant)
** Figure 2 (p.62)
twoway (hist totmp, width(.5) percent color(gs14) yaxis(2)) (line margsapas totmp2, clpattern(solid) clwidth(medium) clcolor(black) yaxis(1)) (line ubapas totmp2, clpattern(dot) clwidth(thin) clcolor(black)) (line lbapas totmp2, clpattern(dot) clwidth(thin) clcolor(black)), ylabel(#9, axis(2) nogrid labsize(2)) ymtick(##0, axis(2)) yline(0,lpattern(solid) lcolor(gs14)) ylabel(#9, axis(1) nogrid labsize(2)) ymtick(##2, axis(1)) yscale(noline alt) yscale(noline alt axis(2)) xscale(noline) legend(off) xtitle("District Magnitude" , size(2.5)) ytitle("Marginal Effect of SPSD Ratio" , axis(1) size(2.5)) ytitle("Percent" , axis(2) size(2.5)) xsca(titlegap(2)) ysca(titlegap(2)) xlabel(#9, labsize(2) nogrid) xmtick(##2) title("Parliamentary Elections between 1987-2011", position(11) size(2.5)) nodraw name(spsd, replace)
twoway (hist totmp, width(.5) percent color(gs14) yaxis(2)) (line margsinc totmp2, clpattern(solid) clwidth(medium) clcolor(black) yaxis(1)) (line ubinc totmp2, clpattern(dot) clwidth(thin) clcolor(black)) (line lbinc totmp2, clpattern(dot) clwidth(thin) clcolor(black)), ylabel(#9, axis(2) nogrid labsize(2)) ymtick(##0, axis(2)) yline(0,lpattern(solid) lcolor(gs14)) ylabel(#9, axis(1) nogrid labsize(2)) ymtick(##2, axis(1)) yscale(noline alt) yscale(noline alt axis(2)) xscale(noline) legend(off) xtitle("District Magnitude" , size(2.5)) ytitle("Marginal Effect of Incumbency Ratio" , axis(1) size(2.5)) ytitle("Percent" , axis(2) size(2.5)) xsca(titlegap(2)) ysca(titlegap(2)) xlabel(#9, labsize(2) nogrid) xmtick(##2) title("Parliamentary Elections between 1987-2011", position(11) size(2.5)) nodraw name(inc, replace)
graph combine spsd inc, ycommon col(1)
graph export Figure2.eps, replace

*** APPENDICES
** Appendix A (p.63)
sutex voteshare totmp cntapas cntinc avvs enlp, labels minmax nobs file(descriptive1.tex) replace
sutex voteshare totmp cntapas cntinc avvs enlp if term!=2002 & term!=1987, labels minmax nobs file(descriptive1.tex) append
sutex voteshare totmp cntapas cntinc avvs enlp if term==2002 | term==1987, labels minmax nobs file(descriptive1.tex) append
** Appendix B (p.63)
preserve
clear
import excel "Raw Data.xlsx", sheet("MP Coding") firstrow case(lower)
gen inc=incumbent-apas
latabstat apas inc, by(term) s(count sum) f(%5.0f)
restore
** Appendix C (p.64)
* Party and district fixed effects
eststo feapas: reg voteshare totmp cntapas c.totmp#c.cntapas enlp i.district i.party2, robust
eststo feinc: reg voteshare totmp cntinc c.totmp#c.cntinc enlp i.district i.party2, robust
* Prais-Winsten regression correcting for panel-specific autocorrelation (AR1)
xtset parid1 year
eststo pwapas: xtpcse voteshare totmp cntapas c.totmp#c.cntapas avvs enlp, correlation(ar1) rhotype(tscorr) pairwise
eststo pwinc: xtpcse voteshare totmp cntinc c.totmp#c.cntinc avvs enlp, correlation(ar1) rhotype(tscorr) pairwise 
* Random effects GLS regression
eststo reapas: xtreg voteshare totmp cntapas c.totmp#c.cntapas avvs enlp, re vce(cluster district) 
eststo reinc: xtreg voteshare totmp cntinc c.totmp#c.cntinc avvs enlp, re vce(cluster district) 
* Generalized linear model with logistic link function
eststo glmapas: glm voteshare totmp cntapas c.totmp#c.cntapas avvs enlp, family(binomial) link(logit) vce(cluster district)
eststo glminc: glm voteshare totmp cntinc c.totmp#c.cntinc avvs enlp, family(binomial) link(logit) vce(cluster district)
esttab feapas feinc pwapas pwinc reapas reinc glmapas glminc using "robust.tex", tex replace b(%10.3f) se scalars("r2 \$R^2\$" "chi2 \$\chi^2\$" "rho \$\rho\$" "sigma_e \$\sigma_e\$" "sigma_u \$\sigma_u\$" "ll Log. Likelihood" "aic AIC" "bic BIC") label mtitles starlevels(* 0.10 ** 0.05  *** 0.01) varlabels(_cons Constant) drop (*party2* *district*)
** Out-of-sample Predictions (in-text discussion of substantial effects on p.62)
set obs 1448
replace totmp=_n-1398 in 1400/1448
foreach x in avvs enlp {
sum `x'
replace `x'=r(mean) in 1400/1448
}
foreach x in cntinc cntapas {
sum `x'
replace `x'=r(min) in 1400/1448
gen int_`x'=`x'*totmp
reg voteshare totmp `x' int_`x' avvs enlp, robust cluster(district)
predict yhatmin`x'
sum `x'
replace `x'=r(mean) in 1400/1448
replace int_`x'=`x'*totmp
predict yhatmean`x'
sum `x'
replace `x'=r(mean)+r(sd) in 1400/1448
replace int_`x'=`x'*totmp
predict yhatmeansd`x'
sum `x'
replace `x'=r(max) in 1400/1448
replace int_`x'=`x'*totmp
predict yhatmax`x'
replace `x'=0.4 in 1400/1448
replace int_`x'=`x'*totmp
predict yhat4`x'
lab var yhatmin`x' "Predicted Values for min(`x')"
lab var yhatmean`x' "Predicted Values for mean(`x')"
lab var yhatmeansd`x' "Predicted Values for mean(`x')+sd(`x')"
lab var yhatmax`x' "Predicted Values for max(`x')"
lab var yhat4`x' "Predicted Values for `x'=0.4"
}
foreach var of varlist yhat* {
qui sum avvs
replace `var'=(`var'-`r(mean)')*100
}
list yhatmincntapas yhatmeancntapas yhatmeansdcntapas yhatmaxcntapas yhat4cntapas in 1400/1448 if totmp<=8
list yhatmincntinc yhatmeancntinc yhatmeansdcntinc yhatmaxcntinc yhat4cntinc in 1400/1448 if totmp>=12
